<!DOCTYPE html />

<html>
<head>
	<title>ModbusTransportFixture.cs</title>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<link href="../../nocco.css" rel="stylesheet" media="all" type="text/css" />
	<script src="../../prettify.js" type="text/javascript"></script>
</head>
<body onload="prettyPrint()">
	<div id="container">
		<div id="background"></div>
			<div id="jump_to">
				Jump To &hellip;
				<div id="jump_wrapper">
					<div id="jump_page">
							<a class="source" href="../../nmodbus4/globalsuppressions.html">
								NModbus4\GlobalSuppressions.cs
							</a>
							<a class="source" href="../../nmodbus4/invalidmodbusrequestexception.html">
								NModbus4\InvalidModbusRequestException.cs
							</a>
							<a class="source" href="../../nmodbus4/modbus.html">
								NModbus4\Modbus.cs
							</a>
							<a class="source" href="../../nmodbus4/slaveexception.html">
								NModbus4\SlaveException.cs
							</a>
							<a class="source" href="../../nmodbus4/data/datastore.html">
								NModbus4\Data\DataStore.cs
							</a>
							<a class="source" href="../../nmodbus4/data/datastoreeventargs.html">
								NModbus4\Data\DataStoreEventArgs.cs
							</a>
							<a class="source" href="../../nmodbus4/data/datastorefactory.html">
								NModbus4\Data\DataStoreFactory.cs
							</a>
							<a class="source" href="../../nmodbus4/data/discretecollection.html">
								NModbus4\Data\DiscreteCollection.cs
							</a>
							<a class="source" href="../../nmodbus4/data/imodbusmessagedatacollection.html">
								NModbus4\Data\IModbusMessageDataCollection.cs
							</a>
							<a class="source" href="../../nmodbus4/data/modbusdatacollection.html">
								NModbus4\Data\ModbusDataCollection.cs
							</a>
							<a class="source" href="../../nmodbus4/data/modbusdatatype.html">
								NModbus4\Data\ModbusDataType.cs
							</a>
							<a class="source" href="../../nmodbus4/data/registercollection.html">
								NModbus4\Data\RegisterCollection.cs
							</a>
							<a class="source" href="../../nmodbus4/device/imodbusmaster.html">
								NModbus4\Device\IModbusMaster.cs
							</a>
							<a class="source" href="../../nmodbus4/device/imodbusserialmaster.html">
								NModbus4\Device\IModbusSerialMaster.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbusdevice.html">
								NModbus4\Device\ModbusDevice.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbusipmaster.html">
								NModbus4\Device\ModbusIpMaster.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbusmaster.html">
								NModbus4\Device\ModbusMaster.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbusmastertcpconnection.html">
								NModbus4\Device\ModbusMasterTcpConnection.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbusserialmaster.html">
								NModbus4\Device\ModbusSerialMaster.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbusserialslave.html">
								NModbus4\Device\ModbusSerialSlave.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbusslave.html">
								NModbus4\Device\ModbusSlave.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbusslaverequesteventargs.html">
								NModbus4\Device\ModbusSlaveRequestEventArgs.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbustcpslave.html">
								NModbus4\Device\ModbusTcpSlave.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbusudpslave.html">
								NModbus4\Device\ModbusUdpSlave.cs
							</a>
							<a class="source" href="../../nmodbus4/device/tcpconnectioneventargs.html">
								NModbus4\Device\TcpConnectionEventArgs.cs
							</a>
							<a class="source" href="../../nmodbus4/extensions/enron/enronmodbus.html">
								NModbus4\Extensions\Enron\EnronModbus.cs
							</a>
							<a class="source" href="../../nmodbus4/io/emptytransport.html">
								NModbus4\IO\EmptyTransport.cs
							</a>
							<a class="source" href="../../nmodbus4/io/istreamresource.html">
								NModbus4\IO\IStreamResource.cs
							</a>
							<a class="source" href="../../nmodbus4/io/modbusasciitransport.html">
								NModbus4\IO\ModbusAsciiTransport.cs
							</a>
							<a class="source" href="../../nmodbus4/io/modbusiptransport.html">
								NModbus4\IO\ModbusIpTransport.cs
							</a>
							<a class="source" href="../../nmodbus4/io/modbusrtutransport.html">
								NModbus4\IO\ModbusRtuTransport.cs
							</a>
							<a class="source" href="../../nmodbus4/io/modbusserialtransport.html">
								NModbus4\IO\ModbusSerialTransport.cs
							</a>
							<a class="source" href="../../nmodbus4/io/modbustransport.html">
								NModbus4\IO\ModbusTransport.cs
							</a>
							<a class="source" href="../../nmodbus4/io/serialportadapter.html">
								NModbus4\IO\SerialPortAdapter.cs
							</a>
							<a class="source" href="../../nmodbus4/io/streamresourceutility.html">
								NModbus4\IO\StreamResourceUtility.cs
							</a>
							<a class="source" href="../../nmodbus4/io/tcpclientadapter.html">
								NModbus4\IO\TcpClientAdapter.cs
							</a>
							<a class="source" href="../../nmodbus4/io/udpclientadapter.html">
								NModbus4\IO\UdpClientAdapter.cs
							</a>
							<a class="source" href="../../nmodbus4/message/abstractmodbusmessage.html">
								NModbus4\Message\AbstractModbusMessage.cs
							</a>
							<a class="source" href="../../nmodbus4/message/abstractmodbusmessagewithdata.html">
								NModbus4\Message\AbstractModbusMessageWithData.cs
							</a>
							<a class="source" href="../../nmodbus4/message/diagnosticsrequestresponse.html">
								NModbus4\Message\DiagnosticsRequestResponse.cs
							</a>
							<a class="source" href="../../nmodbus4/message/imodbusmessage.html">
								NModbus4\Message\IModbusMessage.cs
							</a>
							<a class="source" href="../../nmodbus4/message/imodbusrequest.html">
								NModbus4\Message\IModbusRequest.cs
							</a>
							<a class="source" href="../../nmodbus4/message/modbusmessagefactory.html">
								NModbus4\Message\ModbusMessageFactory.cs
							</a>
							<a class="source" href="../../nmodbus4/message/modbusmessageimpl.html">
								NModbus4\Message\ModbusMessageImpl.cs
							</a>
							<a class="source" href="../../nmodbus4/message/readcoilsinputsrequest.html">
								NModbus4\Message\ReadCoilsInputsRequest.cs
							</a>
							<a class="source" href="../../nmodbus4/message/readcoilsinputsresponse.html">
								NModbus4\Message\ReadCoilsInputsResponse.cs
							</a>
							<a class="source" href="../../nmodbus4/message/readholdinginputregistersrequest.html">
								NModbus4\Message\ReadHoldingInputRegistersRequest.cs
							</a>
							<a class="source" href="../../nmodbus4/message/readholdinginputregistersresponse.html">
								NModbus4\Message\ReadHoldingInputRegistersResponse.cs
							</a>
							<a class="source" href="../../nmodbus4/message/readwritemultipleregistersrequest.html">
								NModbus4\Message\ReadWriteMultipleRegistersRequest.cs
							</a>
							<a class="source" href="../../nmodbus4/message/slaveexceptionresponse.html">
								NModbus4\Message\SlaveExceptionResponse.cs
							</a>
							<a class="source" href="../../nmodbus4/message/writemultiplecoilsrequest.html">
								NModbus4\Message\WriteMultipleCoilsRequest.cs
							</a>
							<a class="source" href="../../nmodbus4/message/writemultiplecoilsresponse.html">
								NModbus4\Message\WriteMultipleCoilsResponse.cs
							</a>
							<a class="source" href="../../nmodbus4/message/writemultipleregistersrequest.html">
								NModbus4\Message\WriteMultipleRegistersRequest.cs
							</a>
							<a class="source" href="../../nmodbus4/message/writemultipleregistersresponse.html">
								NModbus4\Message\WriteMultipleRegistersResponse.cs
							</a>
							<a class="source" href="../../nmodbus4/message/writesinglecoilrequestresponse.html">
								NModbus4\Message\WriteSingleCoilRequestResponse.cs
							</a>
							<a class="source" href="../../nmodbus4/message/writesingleregisterrequestresponse.html">
								NModbus4\Message\WriteSingleRegisterRequestResponse.cs
							</a>
							<a class="source" href="../../nmodbus4/properties/assemblyinfo.html">
								NModbus4\Properties\AssemblyInfo.cs
							</a>
							<a class="source" href="../../nmodbus4/unme.common/disposableutility.html">
								NModbus4\Unme.Common\DisposableUtility.cs
							</a>
							<a class="source" href="../../nmodbus4/unme.common/eventutility.html">
								NModbus4\Unme.Common\EventUtility.cs
							</a>
							<a class="source" href="../../nmodbus4/unme.common/sequenceutility.html">
								NModbus4\Unme.Common\SequenceUtility.cs
							</a>
							<a class="source" href="../../nmodbus4/utility/discriminatedunion.html">
								NModbus4\Utility\DiscriminatedUnion.cs
							</a>
							<a class="source" href="../../nmodbus4/utility/modbusutility.html">
								NModbus4\Utility\ModbusUtility.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/slaveexceptionfixture.html">
								NModbus4.UnitTests\SlaveExceptionFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/data/datastoreeventargsfixture.html">
								NModbus4.UnitTests\Data\DataStoreEventArgsFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/data/datastorefixture.html">
								NModbus4.UnitTests\Data\DataStoreFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/data/discretecollectionfixture.html">
								NModbus4.UnitTests\Data\DiscreteCollectionFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/data/modbusdatacollectionfixture.html">
								NModbus4.UnitTests\Data\ModbusDataCollectionFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/data/registercollectionfixture.html">
								NModbus4.UnitTests\Data\RegisterCollectionFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/device/modbusmasterfixture.html">
								NModbus4.UnitTests\Device\ModbusMasterFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/device/modbusslavefixture.html">
								NModbus4.UnitTests\Device\ModbusSlaveFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/device/tcpconnectioneventargsfixture.html">
								NModbus4.UnitTests\Device\TcpConnectionEventArgsFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/io/modbusasciitransportfixture.html">
								NModbus4.UnitTests\IO\ModbusAsciiTransportFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/io/modbusrtutransportfixture.html">
								NModbus4.UnitTests\IO\ModbusRtuTransportFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/io/modbusserialtransportfixture.html">
								NModbus4.UnitTests\IO\ModbusSerialTransportFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/io/modbustcptransportfixture.html">
								NModbus4.UnitTests\IO\ModbusTcpTransportFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/io/modbustransportfixture.html">
								NModbus4.UnitTests\IO\ModbusTransportFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/io/udpclientadapterfixture.html">
								NModbus4.UnitTests\IO\UdpClientAdapterFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/diagnosticsrequestresponsefixture.html">
								NModbus4.UnitTests\Message\DiagnosticsRequestResponseFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/messageutility.html">
								NModbus4.UnitTests\Message\MessageUtility.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/modbusmessagefactoryfixture.html">
								NModbus4.UnitTests\Message\ModbusMessageFactoryFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/modbusmessagefixture.html">
								NModbus4.UnitTests\Message\ModbusMessageFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/modbusmessageimplfixture.html">
								NModbus4.UnitTests\Message\ModbusMessageImplFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/modbusmessagewithdatafixture.html">
								NModbus4.UnitTests\Message\ModbusMessageWithDataFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/readcoilsinputsrequestfixture.html">
								NModbus4.UnitTests\Message\ReadCoilsInputsRequestFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/readcoilsinputsresponsefixture.html">
								NModbus4.UnitTests\Message\ReadCoilsInputsResponseFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/readholdinginputregistersrequestfixture.html">
								NModbus4.UnitTests\Message\ReadHoldingInputRegistersRequestFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/readholdinginputregistersresponsefixture.html">
								NModbus4.UnitTests\Message\ReadHoldingInputRegistersResponseFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/readwritemultipleregistersrequestfixture.html">
								NModbus4.UnitTests\Message\ReadWriteMultipleRegistersRequestFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/returnquerydatarequestresponsefixture.html">
								NModbus4.UnitTests\Message\ReturnQueryDataRequestResponseFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/slaveexceptionresponsefixture.html">
								NModbus4.UnitTests\Message\SlaveExceptionResponseFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/writemultiplecoilsrequestfixture.html">
								NModbus4.UnitTests\Message\WriteMultipleCoilsRequestFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/writemultiplecoilsresponsefixture.html">
								NModbus4.UnitTests\Message\WriteMultipleCoilsResponseFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/writemultipleregistersrequestfixture.html">
								NModbus4.UnitTests\Message\WriteMultipleRegistersRequestFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/writemultipleregistersresponsefixture.html">
								NModbus4.UnitTests\Message\WriteMultipleRegistersResponseFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/writesinglecoilrequestresponsefixture.html">
								NModbus4.UnitTests\Message\WriteSingleCoilRequestResponseFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/writesingleregisterrequestresponsefixture.html">
								NModbus4.UnitTests\Message\WriteSingleRegisterRequestResponseFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/properties/assemblyinfo.html">
								NModbus4.UnitTests\Properties\AssemblyInfo.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/utility/collectionutilityfixture.html">
								NModbus4.UnitTests\Utility\CollectionUtilityFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/utility/discriminatedunionfixture.html">
								NModbus4.UnitTests\Utility\DiscriminatedUnionFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/utility/modbusutilityfixture.html">
								NModbus4.UnitTests\Utility\ModbusUtilityFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/utility/serialconnectionfixture.html">
								NModbus4.UnitTests\Utility\SerialConnectionFixture.cs
							</a>
							<a class="source" href="../../samples/driver.html">
								Samples\Driver.cs
							</a>
							<a class="source" href="../../samples/properties/assemblyinfo.html">
								Samples\Properties\AssemblyInfo.cs
							</a>
					</div>
				</div>
			</div>
		<table cellpadding="0" cellspacing="0">
			<thead>
				<tr>
					<th class="docs">
						<h1>ModbusTransportFixture.cs</h1>
					</th>
					<th class="code"></th>
				</tr>
			</thead>
			<tbody>
					<tr id="section_1">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_1">&#182;</a>
							</div>
							
						</td>
						<td class="code">
							<pre><code class='prettyprint'>using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Modbus.Data;
using Modbus.IO;
using Modbus.Message;
using Modbus.Utility;
using Rhino.Mocks;

namespace Modbus.UnitTests.IO
{
    using Unme.Common;
    using NUnit.Framework;

    internal delegate ReadCoilsInputsResponse ThrowExceptionDelegate();

    [TestFixture]
    public class ModbusTransportFixture
    {
        [Test]
        public void UnicastMessage()
        {
            MockRepository mocks = new MockRepository();
            ModbusTransport transport = mocks.PartialMock&lt;ModbusTransport&gt;();
            transport.Write(null);
            LastCall.IgnoreArguments();
</code></pre>
						</td>
					</tr>
					<tr id="section_2">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_2">&#182;</a>
							</div>
							<p>read 4 coils from slave id 2</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>            Expect.Call(transport.ReadResponse&lt;ReadCoilsInputsResponse&gt;())
                .Return(new ReadCoilsInputsResponse(Modbus.ReadCoils, 2, 1,
                    new DiscreteCollection(true, false, true, false, false, false, false, false)));
            transport.OnValidateResponse(null, null);
            LastCall.IgnoreArguments();

            mocks.ReplayAll();

            ReadCoilsInputsRequest request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 2, 3, 4);
            ReadCoilsInputsResponse expectedResponse = new ReadCoilsInputsResponse(Modbus.ReadCoils, 2, 1,
                new DiscreteCollection(true, false, true, false, false, false, false, false));
            ReadCoilsInputsResponse response = transport.UnicastMessage&lt;ReadCoilsInputsResponse&gt;(request);
            Assert.AreEqual(expectedResponse.MessageFrame, response.MessageFrame);

            mocks.VerifyAll();
        }

        [Test, ExpectedException(typeof (IOException))]
        public void UnicastMessage_WrongResponseFunctionCode()
        {
            MockRepository mocks = new MockRepository();
            ModbusTransport transport = mocks.PartialMock&lt;ModbusTransport&gt;();
            transport.Write(null);
            LastCall.IgnoreArguments().Repeat.Times(Modbus.DefaultRetries + 1);
</code></pre>
						</td>
					</tr>
					<tr id="section_3">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_3">&#182;</a>
							</div>
							<p>read 4 coils from slave id 2</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>            Expect.Call(transport.ReadResponse&lt;ReadCoilsInputsResponse&gt;())
                .Return(new ReadCoilsInputsResponse(Modbus.ReadCoils, 2, 0, new DiscreteCollection()))
                .Repeat.Times(Modbus.DefaultRetries + 1);

            mocks.ReplayAll();

            ReadCoilsInputsRequest request = new ReadCoilsInputsRequest(Modbus.ReadInputs, 2, 3, 4);
            transport.UnicastMessage&lt;ReadCoilsInputsResponse&gt;(request);

            mocks.VerifyAll();
        }

        [Test, ExpectedException(typeof (SlaveException))]
        public void UnicastMessage_ErrorSlaveException()
        {
            MockRepository mocks = new MockRepository();
            ModbusTransport transport = mocks.PartialMock&lt;ModbusTransport&gt;();
            transport.Write(null);
            LastCall.IgnoreArguments().Repeat.Times(Modbus.DefaultRetries + 1);
            Expect.Call(transport.ReadResponse&lt;ReadCoilsInputsResponse&gt;())
                .Do((ThrowExceptionDelegate) delegate { throw new SlaveException(); });

            mocks.ReplayAll();

            ReadCoilsInputsRequest request = new ReadCoilsInputsRequest(Modbus.ReadInputs, 2, 3, 4);
            transport.UnicastMessage&lt;ReadCoilsInputsResponse&gt;(request);

            mocks.VerifyAll();
        }

</code></pre>
						</td>
					</tr>
					<tr id="section_4">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_4">&#182;</a>
							</div>
							<p>We should reread the response w/o retransmitting the request.</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        [Test]
        public void UnicastMessage_AcknowlegeSlaveException()
        {
            MockRepository mocks = new MockRepository();
            ModbusTransport transport = mocks.PartialMock&lt;ModbusTransport&gt;();

</code></pre>
						</td>
					</tr>
					<tr id="section_5">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_5">&#182;</a>
							</div>
							<p>set the wait to retry property to a small value so the test completes quickly</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>            transport.WaitToRetryMilliseconds = 5;

            transport.Write(null);
            LastCall.IgnoreArguments();

</code></pre>
						</td>
					</tr>
					<tr id="section_6">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_6">&#182;</a>
							</div>
							<p>return a slave exception a greater number of times than number of retries to make sure we aren't just retrying</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>            Expect.Call(transport.ReadResponse&lt;ReadHoldingInputRegistersResponse&gt;())
                .Return(new SlaveExceptionResponse(1, Modbus.ReadHoldingRegisters + Modbus.ExceptionOffset,
                    Modbus.Acknowledge))
                .Repeat.Times(transport.Retries + 1);

            Expect.Call(transport.ReadResponse&lt;ReadHoldingInputRegistersResponse&gt;())
                .Return(new ReadHoldingInputRegistersResponse(Modbus.ReadHoldingRegisters, 1, new RegisterCollection(1)));

            transport.OnValidateResponse(null, null);
            LastCall.IgnoreArguments();

            mocks.ReplayAll();

            ReadHoldingInputRegistersRequest request = new ReadHoldingInputRegistersRequest(
                Modbus.ReadHoldingRegisters, 1, 1, 1);
            ReadHoldingInputRegistersResponse expectedResponse =
                new ReadHoldingInputRegistersResponse(Modbus.ReadHoldingRegisters, 1, new RegisterCollection(1));
            ReadHoldingInputRegistersResponse response =
                transport.UnicastMessage&lt;ReadHoldingInputRegistersResponse&gt;(request);
            Assert.AreEqual(expectedResponse.MessageFrame, response.MessageFrame);

            mocks.VerifyAll();
        }

</code></pre>
						</td>
					</tr>
					<tr id="section_7">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_7">&#182;</a>
							</div>
							<p>We should retransmit the request.</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        [Test]
        public void UnicastMessage_SlaveDeviceBusySlaveException()
        {
            MockRepository mocks = new MockRepository();
            ModbusTransport transport = mocks.PartialMock&lt;ModbusTransport&gt;();

</code></pre>
						</td>
					</tr>
					<tr id="section_8">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_8">&#182;</a>
							</div>
							<p>set the wait to retry property to a small value so the test completes quickly</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>            transport.WaitToRetryMilliseconds = 5;

            transport.Write(null);
            LastCall.IgnoreArguments()
                .Repeat.Times(2);

</code></pre>
						</td>
					</tr>
					<tr id="section_9">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_9">&#182;</a>
							</div>
							<p>return a slave exception a greater number of times than number of retries to make sure we aren't just retrying</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>            Expect.Call(transport.ReadResponse&lt;ReadHoldingInputRegistersResponse&gt;())
                .Return(new SlaveExceptionResponse(1, Modbus.ReadHoldingRegisters + Modbus.ExceptionOffset,
                    Modbus.SlaveDeviceBusy));

            Expect.Call(transport.ReadResponse&lt;ReadHoldingInputRegistersResponse&gt;())
                .Return(new ReadHoldingInputRegistersResponse(Modbus.ReadHoldingRegisters, 1, new RegisterCollection(1)));

            transport.OnValidateResponse(null, null);
            LastCall.IgnoreArguments();

            mocks.ReplayAll();

            ReadHoldingInputRegistersRequest request = new ReadHoldingInputRegistersRequest(
                Modbus.ReadHoldingRegisters, 1, 1, 1);
            ReadHoldingInputRegistersResponse expectedResponse =
                new ReadHoldingInputRegistersResponse(Modbus.ReadHoldingRegisters, 1, new RegisterCollection(1));
            ReadHoldingInputRegistersResponse response =
                transport.UnicastMessage&lt;ReadHoldingInputRegistersResponse&gt;(request);
            Assert.AreEqual(expectedResponse.MessageFrame, response.MessageFrame);

            mocks.VerifyAll();
        }

</code></pre>
						</td>
					</tr>
					<tr id="section_10">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_10">&#182;</a>
							</div>
							<p>We should retransmit the request.</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        [Test]
        public void UnicastMessage_SlaveDeviceBusySlaveExceptionDoesNotFailAfterExceedingRetries()
        {
            MockRepository mocks = new MockRepository();
            ModbusTransport transport = mocks.PartialMock&lt;ModbusTransport&gt;();

</code></pre>
						</td>
					</tr>
					<tr id="section_11">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_11">&#182;</a>
							</div>
							<p>set the wait to retry property to a small value so the test completes quickly</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>            transport.WaitToRetryMilliseconds = 5;

            transport.Write(null);
            LastCall.IgnoreArguments()
                .Repeat.Times(transport.Retries + 1);

</code></pre>
						</td>
					</tr>
					<tr id="section_12">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_12">&#182;</a>
							</div>
							<p>return a slave exception a greater number of times than number of retries to make sure we aren't just retrying</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>            Expect.Call(transport.ReadResponse&lt;ReadHoldingInputRegistersResponse&gt;())
                .Return(new SlaveExceptionResponse(1, Modbus.ReadHoldingRegisters + Modbus.ExceptionOffset,
                    Modbus.SlaveDeviceBusy))
                .Repeat.Times(transport.Retries);

            Expect.Call(transport.ReadResponse&lt;ReadHoldingInputRegistersResponse&gt;())
                .Return(new ReadHoldingInputRegistersResponse(Modbus.ReadHoldingRegisters, 1, new RegisterCollection(1)));

            transport.OnValidateResponse(null, null);
            LastCall.IgnoreArguments();

            mocks.ReplayAll();

            ReadHoldingInputRegistersRequest request = new ReadHoldingInputRegistersRequest(
                Modbus.ReadHoldingRegisters, 1, 1, 1);
            ReadHoldingInputRegistersResponse expectedResponse =
                new ReadHoldingInputRegistersResponse(Modbus.ReadHoldingRegisters, 1, new RegisterCollection(1));
            ReadHoldingInputRegistersResponse response =
                transport.UnicastMessage&lt;ReadHoldingInputRegistersResponse&gt;(request);
            Assert.AreEqual(expectedResponse.MessageFrame, response.MessageFrame);

            mocks.VerifyAll();
        }

        [
            TestCase(typeof (TimeoutException)),
            TestCase(typeof (IOException)),
            TestCase(typeof (NotImplementedException)),
            TestCase(typeof (FormatException))]
        public void UnicastMessage_SingleFailingException(Type exceptionType)
        {
            MockRepository mocks = new MockRepository();
            ModbusTransport transport = mocks.PartialMock&lt;ModbusTransport&gt;();
            transport.Retries = 1;
            transport.Write(null);
            LastCall.IgnoreArguments().Repeat.Times(2);
            Expect.Call(transport.ReadResponse&lt;ReadCoilsInputsResponse&gt;())
                .Do((ThrowExceptionDelegate) delegate { throw (Exception) Activator.CreateInstance(exceptionType); });

            Expect.Call(transport.ReadResponse&lt;ReadCoilsInputsResponse&gt;())
                .Return(new ReadCoilsInputsResponse(Modbus.ReadCoils, 2, 1,
                    new DiscreteCollection(true, false, true, false, false, false, false, false)));

            transport.OnValidateResponse(null, null);
            LastCall.IgnoreArguments();

            mocks.ReplayAll();

            ReadCoilsInputsRequest request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 2, 3, 4);
            transport.UnicastMessage&lt;ReadCoilsInputsResponse&gt;(request);

            mocks.VerifyAll();
        }

        [TestCase(typeof (TimeoutException)),
         TestCase(typeof (IOException)),
         TestCase(typeof (NotImplementedException)),
         TestCase(typeof (FormatException))]
        public void UnicastMessage_TooManyFailingExceptions(Type exceptionType)
        {
            MockRepository mocks = new MockRepository();
            ModbusTransport transport = mocks.PartialMock&lt;ModbusTransport&gt;();

            transport.Write(null);
            LastCall.IgnoreArguments().Repeat.Times(transport.Retries + 1);

            Expect.Call(transport.ReadResponse&lt;ReadCoilsInputsResponse&gt;())
                .Do((ThrowExceptionDelegate) delegate { throw (Exception) Activator.CreateInstance(exceptionType); })
                .Repeat.Times(transport.Retries + 1);

            mocks.ReplayAll();

            ReadCoilsInputsRequest request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 2, 3, 4);

            Assert.Throws(exceptionType, () =&gt; transport.UnicastMessage&lt;ReadCoilsInputsResponse&gt;(request));

            mocks.VerifyAll();
        }

        [Test, ExpectedException(typeof (TimeoutException))]
        public void UnicastMessage_TimeoutException()
        {
            MockRepository mocks = new MockRepository();
            ModbusTransport transport = mocks.PartialMock&lt;ModbusTransport&gt;();
            transport.Write(null);
            LastCall.IgnoreArguments().Repeat.Times(Modbus.DefaultRetries + 1);
            Expect.Call(transport.ReadResponse&lt;ReadCoilsInputsResponse&gt;())
                .Do((ThrowExceptionDelegate) delegate { throw new TimeoutException(); })
                .Repeat.Times(Modbus.DefaultRetries + 1);

            mocks.ReplayAll();

            ReadCoilsInputsRequest request = new ReadCoilsInputsRequest(Modbus.ReadInputs, 2, 3, 4);
            transport.UnicastMessage&lt;ReadCoilsInputsResponse&gt;(request);

            mocks.VerifyAll();
        }

        [Test, ExpectedException(typeof (TimeoutException))]
        public void UnicastMessage_Retries()
        {
            MockRepository mocks = new MockRepository();
            ModbusTransport transport = mocks.PartialMock&lt;ModbusTransport&gt;();
            transport.Retries = 5;
            transport.Write(null);
            LastCall.IgnoreArguments().Repeat.Times(transport.Retries + 1);
            Expect.Call(transport.ReadResponse&lt;ReadCoilsInputsResponse&gt;())
                .Do((ThrowExceptionDelegate) delegate { throw new TimeoutException(); })
                .Repeat.Times(transport.Retries + 1);

            mocks.ReplayAll();

            ReadCoilsInputsRequest request = new ReadCoilsInputsRequest(Modbus.ReadInputs, 2, 3, 4);
            transport.UnicastMessage&lt;ReadCoilsInputsResponse&gt;(request);

            mocks.VerifyAll();
        }

        [Test]
        public void CreateResponse_SlaveException()
        {
            ModbusTransport transport = new ModbusAsciiTransport(MockRepository.GenerateStub&lt;IStreamResource&gt;());
            byte[] frame = {2, 129, 2};
            byte lrc = ModbusUtility.CalculateLrc(frame);
            IModbusMessage message =
                transport.CreateResponse&lt;ReadCoilsInputsResponse&gt;(
                    Enumerable.Concat(frame, new byte[] { lrc }).ToArray());
            Assert.IsTrue(message is SlaveExceptionResponse);
        }

        [Test, ExpectedException(typeof (IOException))]
        public void ValidateResponse_MismatchingFunctionCodes()
        {
            MockRepository mocks = new MockRepository();
            ModbusTransport transport = mocks.PartialMock&lt;ModbusTransport&gt;();

            IModbusMessage request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 1, 1, 1);
            IModbusMessage response = new ReadHoldingInputRegistersResponse(Modbus.ReadHoldingRegisters, 1,
                new RegisterCollection());

            mocks.ReplayAll();
            transport.ValidateResponse(request, response);
            mocks.VerifyAll();
        }

        [Test]
        public void ValidateResponse()
        {
            MockRepository mocks = new MockRepository();
            ModbusTransport transport = mocks.PartialMock&lt;ModbusTransport&gt;();

            IModbusMessage request = new ReadCoilsInputsRequest(Modbus.ReadCoils, 1, 1, 1);
            IModbusMessage response = new ReadCoilsInputsResponse(Modbus.ReadCoils, 1, 1, null);

            transport.OnValidateResponse(null, null);
            LastCall.IgnoreArguments();

            mocks.ReplayAll();
            transport.ValidateResponse(request, response);
            mocks.VerifyAll();
        }
    }
}
</code></pre>
						</td>
					</tr>
			</tbody>
		</table>
	</div>
</body>
</html>
